<template>
	<view>
		<view class="add">
			<view class="hander">
				<view class="text">
					<view class="information">
						<text>温度最大值:{{formvalue.tempmax}}</text>
						<text>湿度最大值:{{formvalue.humimax}}</text>
						<view class="weather">
							<text>天气温度:{{weather.tem}}</text>
							<text>天气湿度:{{weather.humidity}}</text>
						</view>
					</view>
				</view>
			</view>
			<form @submit="submit">
				<view class="item">
					<text>温度:</text>
					<input v-model="formvalue.tempmax" type="number" name="tempmax" placeholder="请输入温度最大值">
				</view>

				<view class="item">
					<text>湿度:</text>
					<input v-model="formvalue.humimax" type="number" name="humimax" placeholder="请输入湿度最大值">
				</view>
				<view class="files-button">
					
				</view>
				<view class="but">
					<button @click="setparam" type="primary">提交</button>
					<button>设置温度</button>
				</view>

			</form>

		</view>
	</view>

</template>

<script>
	export default {
		data() {
			return {
				formvalue: {
					tempmax: "",
					humimax: "",
				},
				token:'version=2022-05-01&res=userid%2F378829&et=1745595703&method=sha1&sign=MbkKStlGzWOmL1%2BbgO3bkDcCYYQ%3D',
				weather: {},
				weatherdetali: {}
			};
		},
		onShow() {
			this.fetchdata();
			this.getweather();
		},
		methods: {
			getweather() {
			
				uni.request({
					url: 'http://v1.yiketianqi.com/api?unescape=1&version=v61&appid=73989722&appsecret=bsGSaD85',
					method: 'GET',
					success: (res) => {
						console.log("天气", res);
						this.weather = res.data
						this.weatherdetali = res.data.aqi
					}
				})
			},
			fetchdata() {
				uni.request({
					url: 'https://iot-api.heclouds.com/thingmodel/query-device-property',
					method: 'GET',
					data: {
						product_id: 'zy1NTZomN9',
						device_name: 'd1'
					},
					header: {
						'authorization': this.token
					},
					success: (res) => {
						console.log(res.data);
						const data = res.data.data;
						const tempmaxValue = data.find(
							(item) => item.identifier === "tempmax"
						).value;
						const humimaxValue = data.find(
							(item) => item.identifier === "humimax"
						).value;
						console.log("温度", tempmaxValue);
						this.formvalue.tempmax = tempmaxValue;
						this.formvalue.humimax = humimaxValue;
					},
				})
			},
			setparam(){
				console.log(this.formvalue.humimax);
				var tempmaxxInt = parseInt(this.formvalue.tempmax);
				var humimaxInt = parseInt(this.formvalue.humimax);
				console.log(humimaxInt);
				uni.request({
					url: 'https://iot-api.heclouds.com/thingmodel/set-device-property',
					method: 'POST',
					data: {
						product_id: 'zy1NTZomN9',
						device_name: 'd1',
						params: {
							"humimax": humimaxInt,
							"tempmax":tempmaxxInt
						}
					},
					header: {
						'authorization': this.token
					},
					success: (res) => {
						console.log(res.data);
						uni.showToast({
							title:"修改成功"
						})
						setTimeout(()=>{
							uni.reLaunch({
								url:"/pages/home/home"
							})
						},100)
					}
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.add {
		padding: 30rpx;
		.information{
			display: flex;
			flex-direction: column;
			margin-bottom: 30rpx;
		}
		.item {
			padding-bottom: 20rpx;
			display: flex;
			flex-direction: row;
			font-size: 47rpx;
			input {
				border-bottom: 1rpx solid #eee;
				height: 80rpx;
				font-size: 47rpx;
				padding: 0 20rpx;
			}
		}

		.but {
			// padding: 20rpx 0;
			margin: 20rpx 50rpx;

			button {
				margin: 20rpx;

			}
		}
	}
</style>